//
//  FriendViewController.m
//  SunXi
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "FriendViewController.h"

@interface FriendViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIView *HeardView;
@end

@implementation FriendViewController{
    UILabel *_Newlabel;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"收到添加" object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"yonghu1"] style:UIBarButtonItemStylePlain target:self action:@selector(presentRightMenuViewController:)];
    [self.navigationItem.rightBarButtonItem setTintColor:BUTTONCOLOR];
    SV.BC = [UIColor whiteColor];
    //创建HeardView
    [self initHearView];
    [self initMyTableView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addmyFriend) name:@"收到添加" object:nil];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (![UserModel shareSingleton].userFirend) {
        //同步请求好友列表
        [ShowMytext showHUDloadView:SV text:@"好友列表加载中..."];
        EMError *error = nil;
        NSArray *buddyList = [[EaseMob sharedInstance].chatManager fetchBuddyListWithError:&error];
        [ShowMytext hiddHUDloadView];
        if (!error) {
            [UserModel shareSingleton].userFirend = buddyList;
            [self.tableView reloadData];
        }else{
            [ShowMytext showAlertViewTitlt:@"错误" message:@"好友列表请求失败请重试" selector:self Block:^{
            }];
        }
    }else{
        [[EaseMob sharedInstance].chatManager asyncFetchBuddyListWithCompletion:^(NSArray *buddyList, EMError *error) {
            if (!error) {
                [UserModel shareSingleton].userFirend = buddyList;
                [self.tableView reloadData];
                [ShowMytext showHUDTextWithTitle:@"好友列表刷新完毕" View:SV];
            }
        } onQueue:nil];
    }
    if (_Newlabel) {
    if ([UserModel shareSingleton].waitFriend.count == 0) {
        _Newlabel.hidden = YES;
    }else{
        _Newlabel.hidden = NO;
        _Newlabel.text = [NSString stringWithFormat:@"%ld",[UserModel shareSingleton].waitFriend.count];
    }
    }
}
- (void)addmyFriend{
    if (_Newlabel) {
        if ([UserModel shareSingleton].waitFriend.count == 0) {
            _Newlabel.hidden = YES;
        }else{
            _Newlabel.hidden = NO;
            _Newlabel.text = [NSString stringWithFormat:@"%ld",[UserModel shareSingleton].waitFriend.count];
        }
    }
}
- (void)initMyTableView{
    self.tableView = ({
        UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        table.delegate = self;
        table.dataSource = self;
        table.tableHeaderView = self.HeardView;
        table.rowHeight = 50;
        table;
    });
    [SV addSubview:self.tableView];
  
}
- (void)initHearView{
    self.HeardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 50)];
    self.HeardView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickFriend)];
    [self.HeardView addGestureRecognizer:tap];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    UIImageView *NewFriendImageView = [[UIImageView alloc]init];
    [self.HeardView addSubview:NewFriendImageView];
    [NewFriendImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.centerY.equalTo(self.HeardView);
        make.left.equalTo(self.HeardView).offset(20);
    }];
    NewFriendImageView.image = [UIImage imageNamed:@"xinhaoyou"];
    UILabel *label = [[UILabel alloc]init];
    [self.HeardView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.HeardView);
        make.left.equalTo(NewFriendImageView.mas_right).offset(20);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    label.text = @"新的好友";
    
    //新好友
    _Newlabel = [[UILabel alloc]init];
    [self.HeardView addSubview:_Newlabel];
    [_Newlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.HeardView);
        make.right.equalTo(self.HeardView).offset(-20);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    _Newlabel.BC = [UIColor redColor];
    _Newlabel.textAlignment = NSTextAlignmentCenter;
    _Newlabel.textColor = [UIColor whiteColor];
    _Newlabel.layer.cornerRadius = 15;
    _Newlabel.layer.masksToBounds  = YES;
    if ([UserModel shareSingleton].waitFriend.count == 0) {
        _Newlabel.hidden = YES;
    }else{
        _Newlabel.text = [NSString stringWithFormat:@"%ld",[UserModel shareSingleton].waitFriend.count];
    }
}

- (void)clickFriend{
    NSLog(@"查看新好友");
    NewFriendViewController *FRVC = [[NewFriendViewController alloc]init];
    [self.navigationController pushViewController:FRVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [UserModel shareSingleton].userFirend.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyfriendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myfriendCell"];
    if (!cell) {
        cell = [[MyfriendTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"myfriendCell"];
    }
    EMBuddy *buddy = [[UserModel shareSingleton].userFirend objectAtIndex:indexPath.row];
    cell.username.text = buddy.username;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewDataiewController *DataVC = [[NewDataiewController alloc]init];
    EMBuddy *buddy = [[UserModel shareSingleton].userFirend objectAtIndex:indexPath.row];
    DataVC.usermane = buddy.username;
    [self.navigationController pushViewController:DataVC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
